﻿using System;
using System.IO;

namespace ElFinder
{
    /// <summary>
    /// Represents a root of file system
    /// </summary>
    public class Root
    {
        #region private
        string _volumeId;
        string _alias;
        DirectoryInfo _directory;
        DirectoryInfo _startPath;
        private int? _maxUploadSize;
        bool _uploadOverwrite;
        string _url;

        string _tmbPath;
        string _tmbUrl;
        int _tmbSize;
        string _tmbBgColor;
        #endregion private

        #region public

        /// <summary>
        /// Initialize new instanse of class <see cref="ElFinder.Root"/>
        /// </summary>
        /// <param name="directory">Directory which will be root</param>
        /// <param name="url">Url to root</param>
        public Root(DirectoryInfo directory, string url)
        {
            if (directory == null)
                throw new ArgumentNullException("directory", "Root directory can not be null");
            if (!directory.Exists)
                throw new ArgumentException("Root directory must exist", "directory");
            _alias = directory.Name;
            _directory = directory;
            _url = url;
            _uploadOverwrite = true;
        }

        /// <summary>
        /// Initialize new instanse of class <see cref="ElFinder.Root"/>
        /// </summary>
        /// <param name="directory">Directory which will be root</param>
        public Root(DirectoryInfo directory) : this(directory, null) { }

        /// <summary>
        /// Gets a autogenerated prefix of root
        /// </summary>
        public string VolumeId
        {
            get { return _volumeId; }
            internal set { _volumeId = value; }
        }

        /// <summary>
        /// Get or sets alias for root. If not set will use directory name of path
        /// </summary>
        public string Alias
        {
            get { return _alias; }
            set { _alias = value; }
        }

        /// <summary>
        /// Get or sets a directory which is root 
        /// </summary>
        public DirectoryInfo Directory
        {
            get { return _directory; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Root directory can not be null", "value");
                if (!value.Exists)
                    throw new ArgumentException("Directory must exist", "directory");
                _directory = value;
            }
        }

        /// <summary>
        /// Get or sets url that points to path directory (also called 'root URL'). 
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Get or sets if root for read only. False by default
        /// </summary>
        public bool IsReadOnly { get; set; }

        /// <summary>
        /// Get or sets if user can only show files (and cannot download). False by default.
        /// Note: if you set url property, than users can access to directory by the provided url
        /// </summary>
        public bool IsShowOnly { get; set; }

        /// <summary>
        /// Get or sets a subfolder of root diretory, which will be start 
        /// </summary>
        public DirectoryInfo StartPath
        {
            get { return _startPath; }
            set
            {
                if (value != null && !value.Exists)
                    throw new ArgumentException("Start directory must exist or can be null", "value");
                _startPath = value;
            }
        }

        /// <summary>
        /// Get or sets maximum upload file size. This size is per files in bytes. 
        /// Note: you still to configure maxupload limits in web.config for whole application
        /// </summary>
        public int? MaxUploadSize
        {
            get { return _maxUploadSize; }
            set
            {
                if (value.HasValue && value.Value < 0)
                    throw new ArgumentException("Max upload size can not be less than zero", "value");
                _maxUploadSize = value;
            }
        }

        /// <summary>
        /// Get or sets maximum upload file size. This size is per files in kb. 
        /// Note: you still to configure maxupload limits in web.config for whole application
        /// </summary>
        public double? MaxUploadSizeInKb
        {
            get { return _maxUploadSize.HasValue ? (double?)(_maxUploadSize.Value / 1024.0) : null; }
            set
            {
                MaxUploadSize = value.HasValue ? (int?)(value * 1024) : null;
            }
        }

        /// <summary>
        /// Get or sets maximum upload file size. This size is per files in Mb. 
        /// Note: you still to configure maxupload limits in web.config for whole application
        /// </summary>
        public double? MaxUploadSizeInMb
        {
            get { return MaxUploadSizeInKb.HasValue ? (double?)(MaxUploadSizeInKb.Value / 1024.0) : null; }
            set
            {
                MaxUploadSizeInKb = value.HasValue ? (int?)(value * 1024) : null;
            }
        }

        /// <summary>
        /// Get or sets if files on upload will replace or give them new names. true - replace old files, false give new names like original_name-number.ext
        /// </summary>
        public bool UploadOverwrite
        {
            get { return _uploadOverwrite; }
            set { _uploadOverwrite = value; }
        }

        #region TODO: implement thumbnails
        public string TmbUrl
        {
            get { return _tmbUrl; }
            set { _tmbUrl = value; }
        }
        public string TmbPath
        {
            get { return _tmbPath; }
            set { _tmbPath = value; }
        }
        public int TmbSize
        {
            get { return _tmbSize; }
            set { _tmbSize = value; }
        }
        public string TmbBgColor
        {
            get { return _tmbBgColor; }
            set { _tmbBgColor = value; }
        }
        #endregion

        #endregion public
    }
}